﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YJYpublicClass;
using System.Windows;
using YJYpublicClass.DataStruct;

namespace YJYpublicClass.Model
{
    public partial class Frm_模型操作 : Form
    {
        ModelCaozuo tempdev = null;
        public Frm_模型操作(ModelCaozuo obj)
        {
            InitializeComponent();
            tempdev = obj;
        }

        private void Frm_模型操作_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = tempdev.modelone;
            //
            //block窗口加载流程
            this.cogToolBlockEditV21.Subject = tempdev.modelone.CCD1vppblock;
            this.cogToolBlockEditV22.Subject = tempdev.modelone.CCD2vppblock;
            this.cogToolBlockEditV23.Subject = tempdev.modelone.CCD3vppblock;

            this.cogDisplayToolbarV21.Display = cogRecordDisplay1;
            this.cogDisplayStatusBarV21.Display = cogRecordDisplay1;
            timer1.Enabled = true;
 
        }

        private void btn_加载模型_Click(object sender, EventArgs e)
        {
            //打开路径，弹窗提示，加载模型会覆盖当前模型
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = tempdev._工程路径;
            openFileDialog1.Filter = "ALL Image Files|*.yjye";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                tempdev.Loadmode(path, out tempdev.modelone);
                //更新显示
                this.propertyGrid1.SelectedObject = tempdev.modelone;
                this.cogToolBlockEditV21.Subject = tempdev.modelone.CCD1vppblock;
                this.cogToolBlockEditV22.Subject = tempdev.modelone.CCD2vppblock;
                this.cogToolBlockEditV23.Subject = tempdev.modelone.CCD3vppblock;
            }

        }

        private void btn_保存模型_Click(object sender, EventArgs e)
        {
            try
            {

                string path = tempdev._工程路径+ "\\" + tempdev.modelone._款式名称 + ".yjye";
                if (File.Exists(path))
                {
                    // DialogResult result = System.Windows.Forms.MessageBox.Show("是否覆盖现有模型?", "是", MessageBoxButtons.YesNo);
                    if (DialogResult.Yes == System.Windows.Forms.MessageBox.Show("是否覆盖现有模型?", "是", MessageBoxButtons.YesNo))
                    {
                        tempdev.modelone.CCD1vppblock = cogToolBlockEditV21.Subject;
                        tempdev.modelone.CCD2vppblock = cogToolBlockEditV22.Subject;
                        tempdev.modelone.CCD3vppblock = cogToolBlockEditV21.Subject;
                        tempdev.Savemode(path, tempdev.modelone);
                    }

                }
                else
                {
                    tempdev.modelone.CCD1vppblock = cogToolBlockEditV21.Subject;
                    tempdev.modelone.CCD2vppblock = cogToolBlockEditV22.Subject;
                    tempdev.modelone.CCD3vppblock = cogToolBlockEditV21.Subject;
                    tempdev.Savemode(path, tempdev.modelone);
                }
                //写数据           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_模型另存_Click(object sender, EventArgs e)
        {
            //修改名字就可以保存
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            try
            {
                tempdev.modelone.CCD1vppblock = cogToolBlockEditV21.Subject;
                tempdev.modelone.CCD2vppblock = cogToolBlockEditV22.Subject;
                tempdev.modelone.CCD3vppblock = cogToolBlockEditV23.Subject;
                saveFileDialog1.Filter = "ALL Image Files|*.yjye";
                saveFileDialog1.InitialDirectory =  tempdev._工程路径;
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string path = saveFileDialog1.FileName;
                    //写数据
                    tempdev.Savemode(path, tempdev.modelone);
                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }
        }

        private void btn_新建模型_Click(object sender, EventArgs e)
        {
            //加载一个模型样板，打开到固定的路径下
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = tempdev._工程路径;
            openFileDialog1.Filter = "ALL Image Files|*.yjye";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                tempdev.Loadmode(path, out tempdev.modelone);
                //更新显示
                this.propertyGrid1.SelectedObject = tempdev.modelone;
                this.cogToolBlockEditV21.Subject = tempdev.modelone.CCD1vppblock;
                this.cogToolBlockEditV22.Subject = tempdev.modelone.CCD2vppblock;
                this.cogToolBlockEditV23.Subject = tempdev.modelone.CCD3vppblock;
            }

        }

        private void btn_实时设置_Click(object sender, EventArgs e)
        {
            try
            {
   
            }
            catch (Exception EX)
            {

                System.Windows.Forms.MessageBox.Show("打开实时失败" + EX.Message);
            }
        }

      
        private void timer1_Tick(object sender, EventArgs e)
        {
            //更新模型    

        }

        private void btn_更新当前模型_Click(object sender, EventArgs e)
        {
            try
            {
              //  tempdev.modelone.mainvppblock = cogToolBlockEditV21.Subject;
                tempdev.modelone.init();
                // tempdev.FifoAcq_tool = tempdev.modelone.FifoAcq_tool;
            }
            catch (Exception ex)
            {

                throw;
            }

        }

        private void btn_清除显示_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }


      

        private void btn_相机3测试_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机3高度获取(cmb_类别选择.SelectedText, out double R);
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机3;
            cogRecordDisplay1.Fit();
        }

        private void btn_相机1测试_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机1角度获取(cmb_类别选择.SelectedText, out double R);
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机1;
            cogRecordDisplay1.Fit();
        }

        private void btn_相机2测试_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机2角度获取(cmb_类别选择.SelectedText,out double R);
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机2;
            cogRecordDisplay1.Fit();
        }

        private void btn_相机1取图_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机1取图操作();
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机1;
            cogRecordDisplay1.Fit();
        }
        private void btn_相机2取图_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机2取图操作();
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机2;
            cogRecordDisplay1.Fit();
        }

        private void btn_相机3取图_Click(object sender, EventArgs e)
        {
            ResultFunc resultFunc = tempdev._相机3取图操作();
            richTextBox1.AppendText(resultFunc.State.ToString());
            richTextBox1.AppendText(resultFunc.mes);
            cogRecordDisplay1.Record =  tempdev.disprecord_相机3;
            cogRecordDisplay1.Fit();
        }

        private void Frm_模型操作_FormClosed(object sender, FormClosedEventArgs e)
        {
            tempdev._配置完成事件?.Invoke();
        }
    }

}
